<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="用户" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="币种" prop="currency">
        <el-select
          v-model="queryParams.currency"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in currencyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="充币时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >充币</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-row>
      <el-col>
        <el-table v-loading="loading" :data="roleList">
          <el-table-column label="ID" prop="id"  v-if="columns[0].visible"/>
          <el-table-column label="用户" prop="username"   v-if="columns[1].visible"/>
          <el-table-column label="币种" prop="currency" show-overflow-tooltip v-if="columns[2].visible"/>
          <el-table-column label="数量" prop="amount"  v-if="columns[3].visible"/>
          <el-table-column label="充币时间" align="center" prop="createTime"  v-if="columns[4].visible">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
<!--          <el-table-column-->
<!--            label="操作"-->
<!--            align="center"-->
<!--            class-name="small-padding fixed-width"-->
<!--          >-->
<!--            <template slot-scope="scope">-->
<!--              <el-button-->
<!--                size="mini"-->
<!--                type="text"-->
<!--                @click="handleUpdate(scope.row)"-->
<!--              >充币</el-button>-->
<!--            </template>-->
<!--          </el-table-column>-->
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户" prop="userId">
          <Fuzzy :child="fuzzyData"  :on-back-parent="onParentGet"></Fuzzy>
        </el-form-item>
        <el-form-item label="充币类型" prop="currency">
          <el-select v-model="form.currency" placeholder="请选择">
            <el-option
              v-for="dict in currencyOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { charginglist, updateCharge} from "@/api/biz"
import { isNumber } from '@/utils/validate'
import Fuzzy from "@/components/Fuzzy";

export default {
  name: "Charging",
  components:{
    Fuzzy
  },
  data() {
    const validAmount = (rule, value, callback) => {
      if (!isNumber(value)) {
        callback(new Error("请输入数字"));
      } else {
        callback();
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 币种状态数据字典
      currencyOptions: [
        {
          dictValue:'BTC',
          dictLabel:'BTC'
        },
        {
          dictValue:'USDT',
          dictLabel:'USDT'
        },
        {
          dictValue:'CRU',
          dictLabel:'CRU'
        },
        {
          dictValue:'XCH',
          dictLabel:'XCH'
        },
        {
          dictValue:'BZZ',
          dictLabel:'BZZ'
        },
        {
          dictValue:'PHA',
          dictLabel:'PHA'
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: undefined,
        currency: undefined
      },
      // 表单参数
      form: {
        id:'',
        userId:'',
        currency:'',
        amount:''
      },
      fuzzyData:{
        echoData:[],
        echoModel:''
      },
      // 列信息
      columns: [
        { key: 0, label: `ID`, visible: true },
        { key: 1, label: `用户`, visible: true },
        { key: 2, label: `币种`, visible: true },
        { key: 3, label: `数量`, visible: true },
        { key: 4, label: `充币时间`, visible: true }
      ],
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "请输入用户名查询", trigger: "blur" }
        ],
        currency: [
          { required: true, message: "请选择币种类型", trigger: "change" }
        ],
        amount: [
          { required: true, message: "请输入充币数量", trigger: "blur" },
          { required: true, validator: validAmount, trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    onParentGet(message){
      this.form.userId = message
    },
    /** 查询后台充币列表 */
    getList() {
      this.loading = true;
      charginglist(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.roleList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 新增充币操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "充币管理";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id:'',
        userId:'',
        currency:'',
        amount:''
      };
      this.fuzzyData = {
        echoData:[],
        echoModel:''
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.id = row.id
      this.form.userId = row.userId
      this.form.currency = row.currency
      this.form.amount = row.amount
      this.open = true;
      this.title = "充币管理";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateCharge(this.form).then(response => {
            this.msgSuccess("充币成功");
            this.open = false;
            this.getList();
          });
        }
      });
    }
  }
};
</script>
